package com.sjgtw.cloud.app.controller;

import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.service.SystemServerService;
import com.sjgtw.cloud.web.path.domain.EmergencyPath;
import com.sjgtw.cloud.web.path.domain.PathPoint;
import com.sjgtw.cloud.web.path.domain.UpperLocation;
import com.sjgtw.cloud.web.path.dto.UpperLocationDTO;
import com.sjgtw.cloud.web.path.service.IEmergencyPathService;
import com.sjgtw.cloud.web.path.service.IPathPointService;
import com.sjgtw.cloud.web.path.service.IUpperLocationService;
import com.sjgtw.cloud.web.path.vo.UpperLocationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 上道地点Controller
 * 
 * @author lvyoufang
 * @date 2024-03-19
 */
@RestController
@RequestMapping("/app/upperLocation")
@Api(tags = "6.4、APP应急路径")
public class AppUpperLocationController extends BaseController
{
    @Autowired
    private IUpperLocationService upperLocationService;

    @Autowired
    private IEmergencyPathService pathService;

    @Autowired
    private IPathPointService pathPointService;

    @Autowired
    private SystemServerService systemServerService;

    @Autowired
    private RedisService redisService;

    /**
     * 查询上道地点列表
     */
    @ApiOperation(value = "查询上道地点列表")
    @GetMapping("/list")
    public TableDataInfo list(UpperLocationDTO upperLocation)
    {
        /*String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);
        if (user == null) {
            user = new LoginUser();
            user.setUserid(14L);
        }

        List<Long> deptIds = new ArrayList<>();
        if (user != null) {
            AjaxResult ajaxResult = systemServerService.getSubDeptIdsByUserId(user.getUserid());
            if (ajaxResult != null) {
                Object object = ajaxResult.get("data");
                if (object != null) {
                    deptIds = (List<Long>) object;
                }
            }
        }
        upperLocation.setDeptIds(deptIds);*/
        startPage();
        List<UpperLocation> list = upperLocationService.selectUpperLocationList(upperLocation);
        if (list.size() > 0) {
            List<Long> upperIds = list.stream().map(UpperLocation::getId).collect(Collectors.toList());
            List<EmergencyPath> paths = pathService.findByUpperIds(upperIds);
            List<PathPoint> points = pathPointService.findByUpperIds(upperIds);
            Map<Long, List<EmergencyPath>> pathMap = paths.stream().collect(Collectors.groupingBy(EmergencyPath::getUpperId));
            Map<Long, List<PathPoint>> pointMap = points.stream().collect(Collectors.groupingBy(PathPoint::getPathId, LinkedHashMap::new, Collectors.toList()));
            for (UpperLocation upper : list) {
                List<EmergencyPath> emergencyPaths = pathMap.get(upper.getId());
                if (emergencyPaths != null && emergencyPaths.size() > 0) {
                    EmergencyPath emergencyPath = emergencyPaths.get(0);
                    List<PathPoint> pathPoints = pointMap.get(emergencyPath.getId());
                    if (pathPoints != null && pathPoints.size() > 0) {
                        PathPoint point = pathPoints.get(pathPoints.size() - 1);
                        upper.setLongitude(point.getLongitude());
                        upper.setLatitude(point.getLatitude());
                    }
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出上道地点列表
     */
   /* @ApiOperation(value = "导出上道地点列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, UpperLocation upperLocation)
    {
        List<UpperLocation> list = upperLocationService.selectUpperLocationList(upperLocation);
        ExcelUtil<UpperLocation> util = new ExcelUtil<UpperLocation>(UpperLocation.class);
	    try {
		    util.exportExcel(response, list, "上道地点数据");
	    } catch (IOException e) {
		    e.printStackTrace();
	    }
    }*/

    /**
     * 获取上道地点详细信息
     */
    @ApiOperation(value = "获取上道地点详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(upperLocationService.selectUpperLocationById(id));
    }

    /**
     * 新增上道地点
     */
  /*  @ApiOperation(value = "新增上道地点")
    @PostMapping
    public AjaxResult add(@RequestBody UpperLocation upperLocation)
    {
        return toAjax(upperLocationService.insertUpperLocation(upperLocation));
    }*/

    /**
     * 修改上道地点
     */
    @ApiOperation(value = "修改上道地点")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody UpperLocationVO upperLocationVO)
    {
        return toAjax(upperLocationService.updateUpperLocation(upperLocationVO));
    }

    /**
     * 删除上道地点
     */
   /* @ApiOperation(value = "删除上道地点")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(upperLocationService.deleteUpperLocationByIds(ids));
    }*/
    /**
     * 删除上道地点
     */
   /* @ApiOperation(value = "删除上道地点")
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(upperLocationService.deleteUpperLocationById(id));
    }*/


//    /**
//     * 删除点位
//     */
//    @ApiOperation(value = "删除点位")
//	@DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable String[] ids)
//    {
//        return toAjax(pathPointService.deletePathPointByIds(ids));
//    }
//
    /**
     * 删除点位
     */
    @ApiOperation(value = "删除点位")
	@PostMapping("/remove")
    public AjaxResult remove(Long id)
    {
        return toAjax(pathPointService.deletePathPointById(id));
    }

}
